<?php

namespace App\Traits;

use Overtrue\EasySms\Message;
use Overtrue\EasySms\Exceptions\GatewayErrorException;

trait SendSms
{
    /**
     * @param $phone 手机号
     * @param Message $message esaysms的message
     * @param $useStr 用途
     */
    public function sendSms($phone, Message $message, $useStr)
    {
        try {
            $data = $message->getData();

            \Cache::put($useStr .  $phone, $data[0], now()->addMinutes($data[1]));

            $easysms = app('easysms');
            $easysms->send($phone, $message);

            return true;
        } catch (GatewayErrorException $e) {
            return abort(500, "请稍后重试");
        } catch (\Exception $e) {
            return abort(500, "请稍后重试");
        }
    }
}
